﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoftwareConsulting.BI11.Controls.Properties;

namespace SoftwareConsulting.BI11.Controls
{
    /// <summary>
    /// Класс сводной таблицы
    /// </summary>
    public partial class SummaryTable : UserControl
    {
        public void Clear()
        {
            SummaryGrid.Rows.Clear();
            SummaryGrid.Columns.Clear();
        }

        /// <summary>
        /// Устанавливает названия столбцов
        /// </summary>
        /// <param name="columnNames"></param>
        public void SetColumns(params string[] columnNames)
        {
            Clear();
            var columns = SummaryGrid.Columns;
            for (int i = 0; i < columnNames.Length; i++)
            {
                columns.Add("column" + i.ToString(), columnNames[i]);                
                var column = columns[columns.Count - 1];
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                column.ReadOnly = true;
                column.ValueType = typeof(string);
                column.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }

        /// <summary>
        /// Устанавливает названия строк
        /// </summary>
        /// <param name="rowNames"></param>
        public void SetRows(params string[] rowNames)
        {            
            var rows = SummaryGrid.Rows;
            rows.Clear();
            for (int i = 0; i < rowNames.Length; i++)
            {
                rows.Add();
                rows[rows.Count - 1].HeaderCell.Value = rowNames[i];
            }
        }

        /// <summary>
        /// Устанавливает значение указанной ячейки
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="colIndex"></param>
        /// <param name="value"></param>
        public void SetValue(int rowIndex, int colIndex, string value)
        {
            var rows = SummaryGrid.Rows;
            var columns = SummaryGrid.Columns;
            if ((rowIndex < 0) || (rowIndex >= rows.Count))
                throw new ArgumentOutOfRangeException("rowIndex");
            if ((colIndex < 0) || (colIndex >= columns.Count))
                throw new ArgumentOutOfRangeException("colIndex");
            rows[rowIndex].Cells[colIndex].Value = value;
        }

        public SummaryTable()
        {
            InitializeComponent();
            SummaryGrid.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
        }
    }
}
